package com.gaga.console.base.utils;

import cn.hutool.core.io.FileUtil;
import com.gaga.common.constant.BvConstants;
import com.gaga.console.core.entity.BvScriptItem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class MyStringUtil {

    public static final Logger logger = LoggerFactory.getLogger(MyStringUtil.class);

    // 匹配数字和非数字部分的正则表达式
    private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+\\.?\\d*)|([^\\d.]+)");

    private static final String SENTENCE_SPLIT_REGEX = "(?<=[。.])";

    /**
     * @param list
     * @return
     */
    public static void sortByName(List<BvScriptItem> list) {
        list.sort(new NaturalComparator());
    }

    static class NaturalComparator implements Comparator<BvScriptItem> {
        @Override
        public int compare(BvScriptItem s1, BvScriptItem s2) {
            Matcher m1 = NUMBER_PATTERN.matcher(s1.getItemName());
            Matcher m2 = NUMBER_PATTERN.matcher(s2.getItemName());

            while (true) {
                boolean f1 = m1.find();
                boolean f2 = m2.find();

                // 同时没有更多部分时相等
                if (!f1 && !f2) return 0;
                // s1部分更少排前面
                if (!f1) return -1;
                // s2部分更少排后面
                if (!f2) return 1;

                String g1 = m1.group();
                String g2 = m2.group();

                // 处理数字比较
                if (isNumber(g1) && isNumber(g2)) {
                    double num1 = Double.parseDouble(g1);
                    double num2 = Double.parseDouble(g2);
                    int cmp = Double.compare(num1, num2);
                    if (cmp != 0) return cmp;
                }
                // 非数字部分直接比较
                else {
                    int cmp = g1.compareToIgnoreCase(g2);
                    if (cmp != 0) return cmp;
                }
            }
        }

        private boolean isNumber(String str) {
            return str.matches("\\d+\\.?\\d*");
        }

        /**
         * @param text
         * @return
         */
//    public static double extractVersion(String text) {
//        Matcher matcher = VERSION_PATTERN.matcher(text);
//        if (matcher.find()) {
//            try {
//                return Double.parseDouble(matcher.group(1));
//            } catch (NumberFormatException e) {
//                // 格式错误排最后
//                return Double.MAX_VALUE;
//            }
//        }
//        // 无版本号排最后
//        return Double.MAX_VALUE;
//    }
    }

    public static String formatParagraphs(String text) {
        if (text == null || text.isEmpty()) return "";

        return Arrays.stream(text.split(SENTENCE_SPLIT_REGEX))
                .map(String::trim)                   // 去除首尾空格
                .filter(s -> !s.isEmpty())           // 过滤空段落
                .map(s -> {
                    if (s.matches(".*[^。.]$")) {    // 检查是否以标点结尾
                        return s + "。";             // 补全缺失的句号
                    }
                    return s;
                })
                .collect(Collectors.joining("\n"));  // 用换行符连接
    }

    public static boolean endsWithIgnoreCase(String str, String suffix) {
        if (str == null || suffix == null) {
            return false;
        }
        if (str.length() < suffix.length()) {
            return false;
        }
        return str.toLowerCase(Locale.ROOT).endsWith(suffix.toLowerCase(Locale.ROOT));
    }

    public static void main(String[] args) {
        System.out.println(endsWithIgnoreCase("文本.7890", "7890"));
    }
}
